package spring.board;

// paramMap 는 전 페이지에서 form 값이 submit 되면서 넘어 온값이 저장
// model 은 처리가 끝나고 view 에 결과 값을 보낼때 쓰는 객체 
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import spring.board.service.LookdService;
import spring.board.vo.LoginVo;
import spring.board.vo.LookdVo;

@Controller
public class LookdController {
	
	@Autowired
	private LookdService lookdService;
	
	@RequestMapping("/login.do")
	public ModelAndView loginTest(@ModelAttribute("loginVo") LoginVo loginVo, ModelMap model) throws Throwable {
		int cnt = lookdService.loginTest(loginVo);
		
		ModelAndView mav = new ModelAndView();
		mav.setViewName("redirect:/list.do");
		
		return mav;
	}
	
	
	@RequestMapping("/today2.do")
	public void today2(@RequestParam Map<String, Object> paramMap, ModelMap model) throws Throwable {
		model.put("today2", lookdService.getToday2());
	}
	
	@RequestMapping("/write.do")
	public void write(@RequestParam Map<String, Object> paramMap, ModelMap model) {
		
	}
	
	@RequestMapping("/insertLookd.do")
	public ModelAndView insertLookd(@RequestParam Map<String, Object> paramMap, ModelMap model) throws Throwable {
		
		System.out.print("id: " + paramMap.get("id") + '\t');
		System.out.print("name: " + paramMap.get("name") + '\t');
		System.out.println("telnumber: " + paramMap.get("telnumber") + '\t');
		
		int cnt = lookdService.insertLookd(paramMap);
		 
		System.out.println(cnt + "건 입력되었습니다.");
		
		ModelAndView mav = new ModelAndView();
		mav.setViewName("redirect:/write.do");
		
		return mav; // view에 전달
	}
	
	
	@RequestMapping("/userList.do")
	public void userList(@RequestParam Map<String, Object> paramMap, ModelMap model) throws Throwable {
		model.put("results", lookdService.getUserList(paramMap));
	}
	
	@RequestMapping("/userVo.do")
	public void getVo(@RequestParam Map<String, Object> paramMap, ModelMap model) throws Throwable {
		// Map<String, Object> result = new HashMap<String, Object>();
		List<LookdVo> users = lookdService.getVo();
		
		System.out.println("***************************************************");
		
		System.out.println(users);
		System.out.println(users.get(0));
		System.out.println(users.get(0).getId());
		
		model.put("user1", users.get(0));
		model.put("user2", users.get(0).getId());
	}
	/*
	@RequestMapping("/lookd_uers.do")
	public void getUsers(@RequestParam Map<String, Object> paramMap, ModelMap model) throws Throwable {
		model.put("users", lookdService.getUsers());
	}
	*/
}
